#*- coding: utf-8 -*
import os
ffmpegPath = os.path.join(os.getcwd(), "ffmpeg.exe")
finishPath =os.path.join(os.getcwd(), "download")
waterPath = os.path.join(os.getcwd(), "water.png")
def changSize(pathUrl):
    if os.path.exists(pathUrl):
        fileName = os.path.basename(pathUrl)
        outpathUrl = os.path.join(finishPath, fileName)
        outpath = "{file_name}_{uuid}.mp4".format(file_name=outpathUrl, uuid="c")
        os.system('%s -i "%s" -i "%s" -filter_complex "overlay=main_w-overlay_w:10" -vcodec libx264 -crf 30 "%s"' % (ffmpegPath,pathUrl,waterPath,outpath))
        os.rename(outpath, outpathUrl)
        return outpathUrl
    else:
        print('changSize:文件不存在')
        return None
def linkPt(pathUrl):
    if os.path.exists(pathUrl):
        fileName = os.path.basename(pathUrl)
        outpathUrl = os.path.join(finishPath, fileName)
        outpath = "{file_name}_{uuid}.mp4".format(file_name=outpathUrl, uuid="L")
        with open(os.path.join(finishPath, outpathUrl + '_merge.txt'), 'a+') as tf:
            tf.write("file '%s'\n" % (os.path.join(os.getcwd(), 'gpt.mp4')))
            tf.write("file '%s'\n" % (outpathUrl))
        # print('ffmpeg -i "%s" -i "%s" -filter_complex "overlay=main_w-overlay_w-10:main_h-overlay_h-10" "%s"' %(pathUrl,waterPath,outpath))
        os.system('%s -f concat -safe 0 -i "%s" -c copy "%s"' %(ffmpegPath,os.path.join(finishPath, outpathUrl + '_merge.txt'),outpath))
        os.remove(os.path.join(finishPath, outpathUrl + '_merge.txt'))
        os.remove(outpathUrl)
        os.rename(outpath, outpathUrl)
    else:
        print('addWater:文件不存在')
        return None
def getPath():
    getpathurl  =  input('请输入视频地址：')
    if getpathurl.split('.')[1] != 'mp4':
        getPath()
    return  getpathurl
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    mp4Path = getPath()
    getnewPath = changSize(mp4Path)
    linkPt(getnewPath)